package net.zjitc.service.impl;

import net.zjitc.context.LocalThreadHolder;
import net.zjitc.mapper.BookMapper;
import net.zjitc.mapper.BookOrderHistoryMapper;
import net.zjitc.pojo.api.ApiResult;
import net.zjitc.pojo.api.PageResult;
import net.zjitc.pojo.api.Result;
import net.zjitc.pojo.dto.query.extend.BookOrderHistoryQueryDto;
import net.zjitc.pojo.dto.query.extend.BookQueryDto;
import net.zjitc.pojo.entity.Book;
import net.zjitc.pojo.entity.BookOrderHistory;
import net.zjitc.pojo.vo.BookOrderHistoryVO;
import net.zjitc.pojo.vo.BookVO;
import net.zjitc.service.BookOrderHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

//书籍预约历史业务逻辑实现
@Service
public class BookOrderHistoryServiceImpl implements BookOrderHistoryService {

    @Resource
    private BookOrderHistoryMapper bookOrderHistoryMapper;
    @Resource
    private BookMapper bookMapper;

    //书籍预约历史新增
    @Override
    public Result<Void> save(BookOrderHistory bookOrderHistory) {
        if (bookOrderHistory.getDeadlineNum() == null){
            return ApiResult.error("请输入本数");
        }
        if(bookOrderHistory.getReturnTime() == null){
            return ApiResult.error("请选择归还日期");
        }
        LocalDate returnTime = bookOrderHistory.getReturnTime();
        LocalDate nowDate = LocalDate.now();
        if (returnTime.isBefore(nowDate)){
            return ApiResult.error("时间不可倒流哦!");
        }
        // 是否已借
        BookOrderHistoryQueryDto dto = new BookOrderHistoryQueryDto();
        dto.setUserId(LocalThreadHolder.getUserId());
        dto.setBookId(bookOrderHistory.getBookId());
        List<BookOrderHistoryVO> bookOrderHistoryVOS = bookOrderHistoryMapper.query(dto);
        for (BookOrderHistoryVO bookOrderHistoryVO : bookOrderHistoryVOS) {
            if (!bookOrderHistoryVO.getIsReturn()) {
                return ApiResult.error("该书籍未归还");
            }
        }
        // 借书数量是否足够
        BookQueryDto bookQueryDto = new BookQueryDto();
        bookQueryDto.setId(bookOrderHistory.getBookId());
        List<BookVO> bookVOS = bookMapper.query(bookQueryDto);
        if (bookVOS.isEmpty()) {
            return ApiResult.error("书籍信息找不到");
        }
        BookVO bookVO = bookVOS.get(0);
        if (bookVO.getNum() - bookOrderHistory.getDeadlineNum() < 0) {
            return ApiResult.error("数量不足");
        }
        Book book = new Book();
        book.setId(bookVO.getId());
        book.setNum(bookVO.getNum() - bookOrderHistory.getDeadlineNum());
        bookMapper.update(book);
        bookOrderHistory.setUserId(LocalThreadHolder.getUserId());
        // 未归还
        bookOrderHistory.setIsReturn(false);
        bookOrderHistory.setCreateTime(LocalDateTime.now());
        bookOrderHistoryMapper.save(bookOrderHistory);
        return ApiResult.success();
    }

    //书籍预约历史删除
    @Override
    public Result<Void> batchDelete(List<Integer> ids) {
        bookOrderHistoryMapper.batchDelete(ids);
        return ApiResult.success();
    }

    //书籍预约历史修改（还书）
    @Override
    public Result<Void> update(BookOrderHistory bookOrderHistory) {
        BookOrderHistoryQueryDto queryDto = new BookOrderHistoryQueryDto();
        queryDto.setId(bookOrderHistory.getId());
        // 不能依赖前端传进来的参数，以数据库存储的为准
        List<BookOrderHistoryVO> bookOrderHistoryVOS = bookOrderHistoryMapper.query(queryDto);
        // 将书籍的库存加回去
        BookQueryDto bookQueryDto = new BookQueryDto();
        bookQueryDto.setId(bookOrderHistoryVOS.get(0).getBookId());
        List<BookVO> bookVOS = bookMapper.query(bookQueryDto);
        BookVO bookVO = bookVOS.get(0);
        Book book = new Book();
        book.setId(bookOrderHistoryVOS.get(0).getBookId());
        book.setNum(bookVO.getNum() + bookOrderHistoryVOS.get(0).getDeadlineNum());
        bookMapper.update(book);
        // 确认归还
        bookOrderHistory.setIsReturn(true);
        bookOrderHistoryMapper.update(bookOrderHistory);
        return ApiResult.success();
    }

    //书籍预约历史查询
    @Override
    public Result<List<BookOrderHistoryVO>> query(BookOrderHistoryQueryDto dto) {
        List<BookOrderHistoryVO> noticeList = bookOrderHistoryMapper.query(dto);
        Integer totalCount = bookOrderHistoryMapper.queryCount(dto);
        return PageResult.success(noticeList, totalCount);
    }

}
